package cn.ac.istic.lkt.atv;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FrmSingleTree extends FrmAbstract{
	private static final long serialVersionUID = -2340039771548191840L;
	List<SentenceTree> stlistf = null;
	List<SentenceTree> stliste = null;
	TreePanelSingle treePanelSingle = null;
	public FrmSingleTree(){		
		super();
		treePanelSingle = new TreePanelSingle();
		
		menuItemLoad.addActionListener(new myListener(this));
		menuItemExportSen.addActionListener(new myListener(this));
		menuItemExportImg.addActionListener(new myListener(this));
		menuItemExit.addActionListener(new myListener(this));
		setVisible(true);

	}


	public void readAndParse(String f){
		ffname = f;
		File ff = new File(f);
		stlistf = SentenceTree.parseFile(ff);		
		//System.out.println(stlistf.size());
		DefaultListModel model = getSenPanel().getModel();
		model.removeAllElements();
		for (int i = 0; i < stlistf.size(); i++) {
			//System.out.println("" + i+" "+stlistf.get(i).getSents());
			model.addElement(stlistf.get(i).getShortSents());
		}
		senPanel.getList().setSelectedIndex(0);
	}


	public void drawTree(int id){
		ParseTree pt = stlistf.get(id).getTrees();
		treePanelSingle.init(pt);
		scrollPane.setViewportView(treePanelSingle);

	}
	
	public void exportImage() {
		if (stlistf == null || stlistf.size() == 0) {
			JOptionPane.showMessageDialog(this,
					"Please Load Tree file first!", "Error!",
					JOptionPane.WARNING_MESSAGE);
			System.err.println("Trees are not loaded yet!");
			return;
		}
		for (int i = 0; i < stlistf.size(); i++) {
			ParseTree pt = stlistf.get(i).getTrees();
			BufferedImage img ;
			
			TreePanelSingle tp = new TreePanelSingle();
			tp.init(pt);
			img = tp.paintImage();			
			
			try {
				File outputfile = new File("C:\\tmp\\img" + i + ".png");
				ImageIO.write(img, "png", outputfile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
	
	class myListener implements ActionListener {
		FrmSingleTree container;

		public myListener(FrmSingleTree app) {
			container = app;
		}

		public void actionPerformed(ActionEvent e) {
			// System.out.print(e.getSource());
			if (e.getSource().equals(menuItemExit)) {
				performExit();
			} else if (e.getSource().equals(menuItemLoad)) {
				container.setMode(S);
				JFileChooser chooser = new JFileChooser();
				if (lastPath != null) {
					File lastd = new File(lastPath);
					chooser.setCurrentDirectory(lastd);
				}
				chooser.setMultiSelectionEnabled(false);
				int option = chooser.showOpenDialog(FrmSingleTree.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					lastPath = chooser.getCurrentDirectory().getAbsolutePath();
					container.readAndParse(lastPath);
					
				}
			}			
			else if (e.getSource().equals(menuItemExportSen)) {
				exportSentence(container.stlistf);
			} else if (e.getSource().equals(menuItemExportImg)) {
				exportImage();
			} else {
				;
			}
		}
	}
	
	
	public static void main(String[] args) {
		FrmSingleTree t2t = new FrmSingleTree();
		
		if (args.length < 3){
			t2t.readAndParse("example/exp.tree.eng");
		}
		//t2t.drawTree(0);
	}
}
